package com.faceye.core.util.result;


import com.faceye.core.util.bean.BeanMapper;
import com.faceye.core.util.result.antd.ProList;
import org.apache.commons.lang3.StringUtils;

/**
 * 数据结构转换器
 */
public class ResultMapper {
    /**
     * 将service层的pageResult 转换为 web层的 ProList(注：for ant desing proTable or proList)
     * @param pageResult
     * @return
     */
    public static <T>  ProList<T> pageResult2ProList(PageResult pageResult,Class<T> destinationClass){
        ProList<T> proList = new ProList<T>();
        if(pageResult!=null){
            proList.setPage(pageResult.getPage());
            proList.setTotal(pageResult.getTotal().intValue());
            proList.setData(BeanMapper.mapList(pageResult.getItems(), destinationClass));
            proList.setSuccess(Boolean.TRUE);
        }
        return proList;
    }

    /**
     * 构建默认响应对像
     * @param msg
     * @param code
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> buildDefaultResult(Boolean success,String msg,Integer code,T data){
        Result<T> res =success!=null && success?new DefaultSuccessResult<T>():new DefaultFailerResult<T>();
        if(code!=null){res.setCode(code);}
        if(StringUtils.isNotEmpty(msg)){res.setMsg(msg);}
        if(data!=null){res.setData(data);}
        return res;
    }
}
